<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/3/23
 * Time: 14:43
 */
namespace Common\Model;

use Think\Page;

class ArticleModel extends BaseModel {


    public function getArticle($title,$pageSize) {
        $opt = array();
        $table = '__ARTICLE__ art';
        $joinChannel = '__CHANNEL__ cha on art.channel_id = cha.id';

        $field = 'cha.title channel , art.id , art.author , art.channel_id , art.time , art.pv , art.title';
        if($title) {
            $opt['art.title'] = array('like','%'.$title.'%');
        }
        if($pageSize){
            $count = $this->table($table)->join($joinChannel)->where($opt)->count();
            $page = new Page($count, $pageSize);
            $data = $this->table($table)->join($joinChannel)->field($field)->where($opt)->order('time desc')->limit($page->firstRow,$page->listRows)->select();
            return array('list' => $data, 'page' => $page->show());
        }

        return array('list'=>$this->table($table)->join($joinChannel)->field($field)->where($opt)->order('time desc')->select());
    }

    public function getArticleById($id) {
        $condition = array(
            'id' => array('in',$id)
        );
        return $this->where($condition)->find();
    }

}